package com.ttai.ui.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.ttai.R;
import com.ttai.model.net.VerifyAccountBean;
import com.ttai.model.net.VerifyIdBean;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class VerifyIdRecycleViewAdapter extends RecyclerView.Adapter {
    public Context context;
    public ArrayList<VerifyIdBean.SellectListBean> idArrayList = new ArrayList<>();
    public int checkamount = 0;
    public VerifyAccountBean.SellectListBean sellectListBean;
    public SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
    public ArrayList<String> checkedAcountList = new ArrayList<>();
    public ArrayList<String> checkedNameList = new ArrayList<>();
    private String data = "";

    public SparseBooleanArray getmSelectedPositions() {
        return mSelectedPositions;
    }

    public void setmSelectedPositions(SparseBooleanArray mSelectedPositions) {
        this.mSelectedPositions = mSelectedPositions;
    }


    public VerifyIdRecycleViewAdapter(Context context, ArrayList<VerifyIdBean.SellectListBean> idArrayList) {
        this.context = context;
        this.idArrayList = idArrayList;
    }

    //根据位置设置条目的状态（添加）
    private void setItemChecked(int position, boolean isChecked) {
        mSelectedPositions.put(position, isChecked);
    }

    private void deleteItemCheck(int position) {
        mSelectedPositions.delete(position);
    }

    //根据位置判断条目是否选中
    private boolean isItemChecked(int position) {
        return mSelectedPositions.get(position);
    }

    public String getData(){
        for (int i = 0; i < checkedAcountList.size(); i++) {
            data = data + checkedNameList.get(i)+","+checkedAcountList.get(i)+"#";
        }
        return data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_verifyid, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        Picasso.with(context)
                .load(idArrayList.get(position).getIcon())
                .placeholder(R.drawable.taisecuritylogosmall)
                .into(((ViewHolder)holder).ivVerifyloginicon);

        ((ViewHolder)holder).tvVerifyidacount.setText(idArrayList.get(position).getAccount());
        ((ViewHolder)holder).tvVerifyidname.setText(idArrayList.get(position).getAccountname());
        ((ViewHolder)holder).cbCheck.setChecked(isItemChecked(position));
        ((ViewHolder) holder).cbCheck.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if (checkamount > 2){
                    ((ViewHolder)holder).cbCheck.setClickable(false);
                    ((ViewHolder)holder).cbCheck.setEnabled(false);
                }else {
                    if (isItemChecked(position)) {
                        setItemChecked(position, false);
                        checkedAcountList.remove(idArrayList.get(position).getAccount());
                        checkedNameList.remove(idArrayList.get(position).getAccountname());
                        checkamount--;
                    } else {
                        setItemChecked(position, true);
                        checkedAcountList.add(idArrayList.get(position).getAccount());
                        checkedNameList.add(idArrayList.get(position).getAccountname());
                        checkamount++;
                    }
                    Toast.makeText(context,"已选择" + checkedAcountList.size() + "项,最多选择三项",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return idArrayList == null ? 0 : idArrayList.size();

    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.cb_check)
        CheckBox cbCheck;
        @Bind(R.id.iv_verifyloginicon)
        ImageView ivVerifyloginicon;
        @Bind(R.id.tv_verifyidname)
        TextView tvVerifyidname;
        @Bind(R.id.tv_verifyidacount)
        TextView tvVerifyidacount;


        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
